#!/bin/bash
#
# Copyright (C) 2021 Francis Deslauriers <francis.deslauriers@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only
#

TEST_DESC="Kernel tracer - function event"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
NUM_TESTS=7

source $TESTDIR/utils/utils.sh

function test_kernel_function_basic()
{
	local TRACE_PATH=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	local SESSION_NAME="kernel_function_basic"
	local EVENT_NAME="my_event_name"
	local TARGET_SYMBOL="lttng_test_filter_event_write"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	lttng_enable_kernel_function_event_ok $SESSION_NAME "$TARGET_SYMBOL" "$EVENT_NAME"

	start_lttng_tracing_ok

	echo 1 > /proc/lttng-test-filter-event

	stop_lttng_tracing_ok

	validate_trace "${EVENT_NAME}_entry" $TRACE_PATH
	validate_trace "${EVENT_NAME}_exit" $TRACE_PATH

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." ||
{
	start_lttng_sessiond_notap
	validate_lttng_modules_present
	modprobe lttng-test

	test_kernel_function_basic

	modprobe --remove lttng-test
	stop_lttng_sessiond_notap
}
